<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Acme\DemoBundle\Entity\TrinhDo;

class TrinhDoController extends Controller
{
	private function getlist ($index,$thongbao) {
		$listTrinhDo =	$this->getDoctrine()
		->getRepository('AcmeDemoBundle:TrinhDo')
		->findByCoditions(10*($index-1),10);
			
		$countTrinhDo = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:TrinhDo')->countAll();
			
		$count = \Count($countTrinhDo)/10;
			
		$mod = \Count($countTrinhDo)%10;
			
		if($mod>0){
			$count++;
		}

		return $this->render(
				'AcmeDemoBundle:Demo:TrinhDo_list.html.twig',
				array('listTrinhDo' => $listTrinhDo,
						'index' => $index,
						'count' => $count,
						'thongbao' => $thongbao
				)
		);
	}
	public function inputAction()
	{
		$request = $this->get('request');
		if ($request->isMethod('POST')) {
			
			$ten = $request->request->get('ten');
			$ma = $request->request->get('ma');
			
			$check = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:TrinhDo')
			->checkMaInsert($ma);
			
			if($check){
				return $this->render(
						'AcmeDemoBundle:Demo:TrinhDo_input.html.twig',
						array('exist' => "ok",
								'ma' => $ma,
								'ten' => $ten
			
						)
				);
			}
			
			$TrinhDo = new TrinhDo();
			$TrinhDo->setTen($ten);
			$TrinhDo->setMa($ma);
			$TrinhDo->setCreated(new \DateTime('now'));
			$this->getDoctrine()
			->getRepository('AcmeDemoBundle:TrinhDo')
			->saveTrinhDo($TrinhDo);


			return $this->getlist(1,"ok");

		}
		return $this->render(
				'AcmeDemoBundle:Demo:TrinhDo_input.html.twig',
						array('exist' => null,
								'ma' => "",
								'ten' => ""
			
						)
		);
	}
	public function listAction($index)
	{
		return $this->getlist($index,null);
		
	}
	public function detailAction($id)
	{
		$TrinhDo = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:TrinhDo')
		->find($id);
		 
		return $this->render(
				'AcmeDemoBundle:Demo:TrinhDo_edit.html.twig',
				array('TrinhDo' => $TrinhDo,'exist' => null)
		);
	}
	public function editAction($id)
	{
		$TrinhDo = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:TrinhDo')
		->find($id);
		$request = $this->get('request');
		$ma = $request->request->get('ma');
		$ten = $request->request->get('ten');
		
		if($TrinhDo->getMa() != $ma) {
			$Check = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:TrinhDo')
			->checkMaUpdate($ma,$TrinhDo->getMa());
			if($Check){
				return $this->render(
						'AcmeDemoBundle:Demo:TrinhDo_edit.html.twig',
						array('TrinhDo' => $TrinhDo,'exist' => 'ok')
				);
			}
		}

		$TrinhDo->setTen($ten);
		$TrinhDo->setMa($ma);
		
		$TrinhDo->setUpdated(new \DateTime('now'));
		$this->getDoctrine()
		->getRepository('AcmeDemoBundle:TrinhDo')
		->saveTrinhDo($TrinhDo);

		return $this->getlist(1,"ok");


	}
	public function delAction($id)
	{
		$TrinhDo = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:TrinhDo')
		->find($id);
		
		$TrinhDo->setDeleted(new \DateTime('now'));
		
		$this->getDoctrine()
		->getRepository('AcmeDemoBundle:TrinhDo')
		->saveTrinhDo($TrinhDo);

		return $this->getlist(1,"ok");
		
	}

}
